# -*- encoding=utf8 -*-

__author__ = "code哥"
import asyncio
from sanic.blueprints import Blueprint
from sanic_openapi import doc
from conn_pool import get_redis, REDIS_LIST_KEY

rs = get_redis()

blueprint = Blueprint(__name__, '/api')


@doc.summary("websocket 推送产生图片")
async def feed(request, ws):
    while True:
        msg = rs.lpop(REDIS_LIST_KEY)
        if msg is not None and len(str(msg)) > 0 and not str(msg).endswith('.txt'):
            print('send: ', str(msg))
            await ws.send(str(msg).split('/ruyangmao/')[-1])
        await asyncio.sleep(0.1)

blueprint.add_websocket_route(feed, '/echo_ws')
